package com.zbkj.admin.controller.platform.assistant.service.impl;

import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zbkj.admin.controller.platform.assistant.service.AssistantConversationService;
import com.zbkj.admin.controller.platform.assistant.service.AssistantConversationTransferService;
import com.zbkj.admin.controller.platform.assistant.service.AssistantGroupAccountService;
import com.zbkj.admin.controller.platform.assistant.service.AssistantMessageService;
import com.zbkj.common.constants.AssistantConstants;
import com.zbkj.common.exception.CrmebException;
import com.zbkj.common.model.admin.SystemAdmin;
import com.zbkj.common.model.assistant.AssistantConversation;
import com.zbkj.common.model.assistant.AssistantConversationTransfer;
import com.zbkj.common.model.assistant.AssistantGroupAccount;
import com.zbkj.common.page.CommonPage;
import com.zbkj.common.request.PageParamRequest;
import com.zbkj.common.result.AssistantResultCode;
import com.zbkj.common.utils.SecurityUtil;
import com.zbkj.service.assistant.model.request.AssistantConversationTransferRequest;
import com.zbkj.service.assistant.model.request.AssistantConversationTransferSearchRequest;
import com.zbkj.service.assistant.service.AssistantConversationTransferBaseServiceImpl;
import com.zbkj.service.service.SystemAdminService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Slf4j
@Primary
@Service
@RequiredArgsConstructor
public class AssistantConversationTransferServiceImpl extends AssistantConversationTransferBaseServiceImpl implements AssistantConversationTransferService {
    @Autowired
    private AssistantConversationService assistantConversationService;
    @Autowired
    private AssistantGroupAccountService assistantGroupAccountService;
    @Autowired
    private SystemAdminService systemAdminService;
    @Autowired
    private AssistantMessageService assistantMessageService;

    @Override
    @Transactional
    public void transfer(AssistantConversationTransferRequest request) {
        SystemAdmin admin = SecurityUtil.getLoginUserVo().getUser();
        AssistantGroupAccount fromAccount = assistantGroupAccountService.getByAccountId(admin.getId());

        AssistantConversation conversation = assistantConversationService.getById(request.getConversationId());
        if (ObjUtil.isNull(conversation)) {
            throw new CrmebException(AssistantResultCode.ASSISTANT_CONVERSATION_NOT_EXIST);
        }
        if (!conversation.getServiceAccountId().equals(fromAccount.getAccountId())) {
            throw new CrmebException(AssistantResultCode.CONVERSATION_NOT_YOURS);
        }
        if (request.getToAccountId().equals(fromAccount.getAccountId())) {
            throw new CrmebException(AssistantResultCode.CONVERSATION_CANT_TRANSFER_TO_SELF);
        }

        AssistantGroupAccount toAccount = assistantGroupAccountService.getByAccountId(request.getToAccountId());
        if (ObjUtil.isNull(toAccount)) {
            throw new CrmebException(AssistantResultCode.ASSISTANT_GROUP_ACCOUNT_NOT_EXIST);
        }

        SystemAdmin toAdmin = systemAdminService.getById(request.getToAccountId());
        conversation = assistantConversationService.updateTransfer(conversation, toAccount);

        AssistantConversationTransfer param = new AssistantConversationTransfer()
            .setConversationId(conversation.getId())
            .setFromAccountId(fromAccount.getAccountId())
            .setFromNickname(fromAccount.getNickname())
            .setFromAvatar(fromAccount.getAvatar())
            .setFromAccount(admin.getAccount())
            .setToAccountId(toAccount.getAccountId())
            .setToNickname(toAccount.getNickname())
            .setToAvatar(toAccount.getAvatar())
            .setToAccount(toAdmin.getAccount())
            .setReason(request.getReason());
        super.save(param);

        // 给用户发送一条转接消息
        String content = StrUtil.format("您的会话已转接由客服 {} 进行对接", toAccount.getNickname());
        assistantMessageService.sendBySystem(conversation, AssistantConstants.Message.CONTENT_TYPE_SYSTEM, content, true, AssistantConstants.Message.CONTENT_TYPE_SERVICE_REFRESH_CONVERSATION);
    }

    @Override
    public PageInfo<AssistantConversationTransfer> getPage(AssistantConversationTransferSearchRequest request, PageParamRequest pageParamRequest) {
        Page<AssistantConversationTransfer> page = PageHelper.startPage(pageParamRequest.getPage(), pageParamRequest.getLimit());
        List<AssistantConversationTransfer> list = super.lambdaQuery()
            .eq(ObjUtil.isNotEmpty(request.getConversationId()), AssistantConversationTransfer::getConversationId, request.getConversationId())
            .list();
        return CommonPage.copyPageInfo(page, list);
    }
}

